﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class InitController : Controller
    {
        //
        // GET: /Init/

        public ActionResult Restart()
        {
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {

                    if (BenefitHelper.Sys.ServerStatus.GetServerStatus() == Convert.ToInt32(BenefitHelper.Sys._ServerStatus.Bussy))
                    {
                        BenefitHelper.Sys.ServerStatus.SetServerStatus(Convert.ToInt32(BenefitHelper.Sys._ServerStatus.Free));
                    }
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Index()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();

            if (BenefitHelper.Sys.Company.HasInited(db))
            {
                db.Dispose();
                return View();
            }
            else
            {
                return RedirectToAction("CompanyManager");
            }

        }

        public ActionResult GetUnRegistAccount()
        {

            return View();
        }

        public ActionResult AddNewOperator()
        {
            //   BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            string operId = cookie.GetCookie("LoginUserId");
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.Operator().GetUnDeletedOperators(db);
                    ViewBag.operators = operators;

                    List<BenefitHelper.Sys.Groups> groups = new BenefitHelper.Sys.Groups().GetGroups(db);
                    ViewBag.groups = groups;

                    List<BenefitHelper.Sys.GroupOperators> gos = new BenefitHelper.Sys.GroupOperators().GetList(db);
                    ViewBag.gos = gos;

                    if (!string.IsNullOrEmpty(Request.QueryString["type"]))
                    {
                        ViewBag.iid = "1";
                    }
                    else
                    {
                        ViewBag.iid = "";
                    }

                    db.Dispose();
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult AddNewGroups()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    List<BenefitHelper.Sys.Groups> groups = new BenefitHelper.Sys.Groups().GetGroups(db);
                    ViewBag.groups = groups;

                    List<BenefitHelper.Sys.GroupMaster> masters = new List<BenefitHelper.Sys.GroupMaster>();
                    masters = new BenefitHelper.Sys.GroupMaster().GetList(db);
                    ViewBag.masters = masters;

                    List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.Operator().GetList(db);
                    ViewBag.operators = operators;
                    if (!string.IsNullOrEmpty(Request.QueryString["type"]))
                    {
                        ViewBag.iid = "1";
                    }
                    else
                    {
                        ViewBag.iid = "";
                    }
                    db.Dispose();
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult PowerManager()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    var query = new BenefitHelper.Sys.Operator().GetUnDeletedOperators(db);
                    ViewBag.operators = query;

                    var _query = new BenefitHelper.Sys.User().GetList(db);
                    ViewBag.users = _query;

                    var opdaygroup = new BenefitHelper.Sys.DayGroupOperators().GetList(db);
                    ViewBag.opdaygroup = opdaygroup;
                    db.Dispose();
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult OperatorManager(int id)
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            //   string usertype = cookie.GetCookie("usertype");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    BenefitHelper.Sys.Operator op = new BenefitHelper.Sys.Operator().GetModel(id, db);
                    ViewBag.op = op;
                    List<Common.Module.Permissions.Model.T_GroupManager> gms = db.GroupManager.ToList();
                    ViewBag.gms = gms;


                    List<Common.Module.Permissions.Model.T_MasterGroup> mgs = db.MasterGroup.Where(a => a.UserType == 1).Where(a => a.MasterId == id).ToList();//获取用户所在的所有的组的编号

                    ViewBag.groups = mgs;

                    db.Dispose();
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult CompanyManager()
        {
            //Common.Web.Cookie cookie = new Common.Web.Cookie();
            //string operId = cookie.GetCookie("LoginUserId");
            //if (!string.IsNullOrEmpty(operId))
            //{
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            //if (BenefitHelper.Sys.VisitPrower.HasAdminPrower(Convert.ToInt32(operId), db))
            //{
            var query = db.Company.Count();
            if (query == 0)
            {
                ViewBag.company = new BenefitHelper.Sys.Company();
            }
            else
            {
                ViewBag.company = db.Company.First();
            }
            db.Dispose();

            ViewBag.iid = Request.QueryString["type"];
            return View();
            //    }
            //    else
            //    {
            //        return RedirectToAction("Index");
            //    }
            //}
            //else
            //{
            //    return RedirectToAction("Index");
            //}
        }

        [HttpPost]
        public ActionResult CompanyManager(int id)
        {
            //Common.Web.Cookie cookie = new Common.Web.Cookie();
            //string operId = cookie.GetCookie("LoginUserId");
            //if (!string.IsNullOrEmpty(operId))
            //{
            //   "aaa".Debug();
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            //if (BenefitHelper.Sys.VisitPrower.HasAdminPrower(Convert.ToInt32(operId), db))
            //{
            string companyName = Request.Form["companyName"];
            companyName.Debug();
            BenefitHelper.Sys.Company company = new BenefitHelper.Sys.Company();
            company.CompanyName = companyName;
            //   BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var query = db.Company.Count();
            if (query == 0)
            {
                db.Company.Add(company);
            }
            else
            {
                var _query = db.Company.First();
                _query.CompanyName = company.CompanyName;
            }
            db.SaveChanges();
            if (!string.IsNullOrEmpty(Request.Form["iid"]))
            {
                return RedirectToAction("More", "Init");
            }
            else
            {
                return RedirectToAction("Index");
            }
            //    }
            //    else
            //    {
            //        return RedirectToAction("Index");
            //    }
            //}
            //else
            //{
            //    return RedirectToAction("Index");
            //}

        }

        [HttpPost]
        public ActionResult OperatorManager()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    int opid = Convert.ToInt32(Request.Form["id"]);
                    string ids = Request.Form["groups"];
                    var query = db.MasterGroup.Where(a => a.MasterId == opid).Where(a => a.UserType == 1).ToList();
                    foreach (Common.Module.Permissions.Model.T_MasterGroup mg in query)
                    {
                        db.MasterGroup.Remove(mg);
                    }
                    string[] groups = ids.Split(new char[] { ',' });
                    foreach (string s in groups)
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            int _id = Convert.ToInt32(s);
                            Common.Module.Permissions.Model.T_MasterGroup mg = new Common.Module.Permissions.Model.T_MasterGroup();
                            mg.GroupId = _id;
                            mg.MasterId = opid;
                            mg.UserType = 1;
                            db.MasterGroup.Add(mg);
                        }
                    }
                    new BenefitHelper.Sys.OperatorCouldSeeGroups().InitOperatorCouldSeeGroups(db);
                    db.SaveChanges();

                    db.Dispose();
                    return RedirectToAction("PowerManager");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Manager(int id)
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            //   string usertype = cookie.GetCookie("usertype");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    BenefitHelper.Sys.User op = new BenefitHelper.Sys.User().GetModel(id, db);
                    ViewBag.op = op;
                    List<Common.Module.Permissions.Model.T_GroupManager> gms = db.GroupManager.ToList();
                    ViewBag.gms = gms;


                    List<Common.Module.Permissions.Model.T_MasterGroup> mgs = db.MasterGroup.Where(a => a.UserType == 2).Where(a => a.MasterId == id).ToList();//获取用户所在的所有的组的编号

                    ViewBag.groups = mgs;

                    db.Dispose();
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        [HttpPost]
        public ActionResult Manager()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    int opid = Convert.ToInt32(Request.Form["id"]);
                    string ids = Request.Form["groups"];
                    var query = db.MasterGroup.Where(a => a.MasterId == opid).Where(a => a.UserType == 2).ToList();
                    foreach (Common.Module.Permissions.Model.T_MasterGroup mg in query)
                    {
                        db.MasterGroup.Remove(mg);
                    }
                    string[] groups = ids.Split(new char[] { ',' });
                    foreach (string s in groups)
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            int _id = Convert.ToInt32(s);
                            Common.Module.Permissions.Model.T_MasterGroup mg = new Common.Module.Permissions.Model.T_MasterGroup();
                            mg.GroupId = _id;
                            mg.MasterId = opid;
                            mg.UserType = 2;
                            db.MasterGroup.Add(mg);
                        }
                    }
                    new BenefitHelper.Sys.OperatorCouldSeeGroups().InitOperatorCouldSeeGroups(db);
                    db.SaveChanges();

                    db.Dispose();
                    return RedirectToAction("PowerManager");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        /// <summary>
        /// 角色管理
        /// </summary>
        /// <returns></returns>
        public ActionResult UserTypeManager()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    var query = db.GroupManager.ToList();
                    ViewBag.groupmaster = query;

                    db.Dispose();
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult MasterCreate()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    var query = db.Action.ToList();
                    ViewBag.actions = query;
                    var operators = new BenefitHelper.Sys.Operator().GetUnDeletedOperators(db);
                    ViewBag.operators = operators;
                    var groups = new BenefitHelper.Sys.Groups().GetGroups(db);
                    ViewBag.groups = groups;
                    db.Dispose();
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        public ActionResult MasterCreate(int id)
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    string t_action = Request.Form["t_action"];
                    string groupname = Request.Form["GroupName"];
                    string description = Request.Form["Description"];
                    Common.Module.Permissions.Model.T_GroupManager gm = new Common.Module.Permissions.Model.T_GroupManager();
                    gm.Description = groupname;
                    gm.GroupName = groupname;
                    //     BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
                    db.GroupManager.Add(gm);
                    db.SaveChanges();
                    int gid = gm.Id;
                    string[] ids = t_action.Split(new char[] { ',' });
                    foreach (string _id in ids)
                    {
                        if (!string.IsNullOrEmpty(_id))
                        {
                            Common.Module.Permissions.Model.T_ActionGroup ag = new Common.Module.Permissions.Model.T_ActionGroup();
                            ag.ActionId = _id;
                            ag.GroupId = gid;
                            db.ActionGroup.Add(ag);
                        }
                    }
                    db.SaveChanges();
                    db.Dispose();
                    return RedirectToAction("UserTypeManager");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult MasterEdit(int id)
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    var query = db.Action.ToList();
                    ViewBag.actions = query;



                    Common.Module.Permissions.Model.T_GroupManager gm = db.GroupManager.Find(id);

                    var opactionquery = db.ActionGroup.Where(a => a.GroupId == id).ToList();
                    ViewBag.opactionquery = opactionquery;
                    var operators = new BenefitHelper.Sys.Operator().GetUnDeletedOperators(db);
                    ViewBag.operators = operators;
                    var groups = new BenefitHelper.Sys.Groups().GetGroups(db);
                    ViewBag.groups = groups;
                    db.Dispose();
                    return View(gm);
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        [HttpPost]
        public ActionResult MasterEdit()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    int id = Convert.ToInt32(Request.Form["Id"]);
                    Common.Module.Permissions.Model.T_GroupManager gm = db.GroupManager.Find(id);
                    gm.Description = Request.Form["Description"];
                    gm.GroupName = Request.Form["GroupName"];

                    var query = db.ActionGroup.Where(a => a.GroupId == id).ToList();
                    foreach (Common.Module.Permissions.Model.T_ActionGroup ag in query)
                    {
                        db.ActionGroup.Remove(ag);
                    }
                    db.SaveChanges();
                    string t_action = Request.Form["t_action"];
                    string[] ids = t_action.Split(new char[] { ',' });
                    foreach (string _id in ids)
                    {
                        if (!string.IsNullOrEmpty(_id))
                        {
                            Common.Module.Permissions.Model.T_ActionGroup ag = new Common.Module.Permissions.Model.T_ActionGroup();
                            ag.ActionId = _id;
                            ag.GroupId = id;
                            db.ActionGroup.Add(ag);
                        }
                    }
                    //  new BenefitHelper.Sys.OperatorCouldSeeGroups().Delete(db);
                    new BenefitHelper.Sys.OperatorCouldSeeGroups().InitOperatorCouldSeeGroups(db);
                    db.SaveChanges();
                    db.Dispose();
                    return RedirectToAction("UserTypeManager");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        public ActionResult AddNewOperator(string param)
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.Operator().GetUnDeletedOperators(db);
                    List<BenefitHelper.Sys.GroupOperators> gos = new List<BenefitHelper.Sys.GroupOperators>();
                    foreach (BenefitHelper.Sys.Operator op in operators)
                    {
                        BenefitHelper.Sys.GroupOperators go = new BenefitHelper.Sys.GroupOperators();
                        go.OperatorId = op.Id;
                        go.GroupId = Convert.ToInt32(Request.Form[op.Id.ToString()]);
                        gos.Add(go);
                        //   new BenefitHelper.Sys.OperatorCouldSeeGroups().ResetOperatorColuldSeeGroups(op.Id, go.GroupId, db);
                    }
                    new BenefitHelper.Sys.GroupOperators().ResetOperatorGroups(gos, db);
                    db.Dispose();
                    if (!string.IsNullOrEmpty(Request.Form["iid"]))
                    {
                        return RedirectToAction("AddNewOperator?type=1");
                    }
                    else
                    {
                        return RedirectToAction("AddNewAccount");
                    }
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult AddNewAccount()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    List<BenefitHelper.Data.ServerData.T_Account> accounts = new List<BenefitHelper.Data.ServerData.T_Account>();
                    BenefitHelper.Data.ServerData.T_Account account = new BenefitHelper.Data.ServerData.T_Account();
                    accounts = account.GetList();


                    //    BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
                    List<BenefitHelper.Sys.Account> serverAccounts = new BenefitHelper.Sys.Account().GetUnDeleteList(db);
                    ViewBag.serverAccounts = serverAccounts;
                    List<BenefitHelper.Data.ServerData.T_Account> unSavedServerAccount = new List<BenefitHelper.Data.ServerData.T_Account>();
                    int connectionStringCount = System.Configuration.ConfigurationManager.ConnectionStrings.Count;
                    for (int i = 0; i < connectionStringCount; i++)
                    {
                        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString;
                        if (System.Configuration.ConfigurationManager.ConnectionStrings[i].ProviderName.Equals("System.Data.OracleClient"))
                        {
                            Common.ADO.Tools.OracleHelper.connectionString = connectionString;
                            List<BenefitHelper.Data.ServerData.T_Account> accountList = new BenefitHelper.Data.ServerData.T_Account().GetUnRegistAccounts(db);
                            foreach (BenefitHelper.Data.ServerData.T_Account _account in accountList)
                            {
                                unSavedServerAccount.Add(_account);
                            }
                        }
                    }


                    //  List<BenefitHelper.Data.ServerData.T_Account> unSavedServerAccount = new BenefitHelper.Data.ServerData.T_Account().GetUnRegistAccounts(db);
                    ViewBag.unSavedServerAccount = unSavedServerAccount;
                    List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.Operator().GetUnDeletedOperators(db);
                    ViewBag.operators = operators;
                    List<BenefitHelper.Sys.OperatorAccount> oas = new BenefitHelper.Sys.OperatorAccount().GetList(db);
                    ViewBag.oas = oas;
                    db.Dispose();
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        public ActionResult AddNewAccount(string param)
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    List<BenefitHelper.Sys.Account> serverAccounts = new BenefitHelper.Sys.Account().GetList();
                    List<BenefitHelper.Sys.OperatorAccount> oas = new List<BenefitHelper.Sys.OperatorAccount>();
                    foreach (BenefitHelper.Sys.Account account in serverAccounts)
                    {
                        BenefitHelper.Sys.OperatorAccount oa = new BenefitHelper.Sys.OperatorAccount();
                        oa.OperatorId = Convert.ToInt32(Request.Form[account.Id.ToString()]);
                        oa.AccountId = account.Id;
                        oas.Add(oa);
                    }
                    new BenefitHelper.Sys.OperatorAccount().ResetOperatorAccount(oas, db);
                    db.Dispose();
                    return RedirectToAction("SelectDate");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult SelectDate()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    DateTime pdate = DateTime.Now;
                    ViewBag.date = pdate.Year + "-" + (pdate.Month > 9 ? pdate.Month + "" : "0" + pdate.Month) + "-" + (pdate.Day > 9 ? pdate.Day + "" : "0" + pdate.Day);
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        public ActionResult SelectDate(string key)
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    DateTime pdate = DateTime.Now;
                    DateTime.TryParse(Request.Form["key"], out pdate);
                    string querydate = pdate.Year + "" + (pdate.Month > 9 ? pdate.Month + "" : "0" + pdate.Month) + "" + (pdate.Day > 9 ? pdate.Day + "" : "0" + pdate.Day);
                    new BenefitHelper.EnterPoint().Work(querydate);
                    return RedirectToAction("Working");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Working()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult More()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Delete()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    DateTime pdate = DateTime.Now;
                    ViewBag.date = pdate.Year + "-" + (pdate.Month > 9 ? pdate.Month + "" : "0" + pdate.Month) + "-" + (pdate.Day > 9 ? pdate.Day + "" : "0" + pdate.Day);
                    return View();
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        [HttpPost]
        public ActionResult DeleteData()
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string operId = cookie.GetCookie("LoginUserId");
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var counts = db.TradeHistorys.Count();
            if (couldVisitWithoutLogin(operId, counts))
            {
                if (couldVisitAdmin(Convert.ToInt32(operId), db, counts))
                {
                    DateTime pdate = DateTime.Now;
                    DateTime.TryParse(Request.Form["key"], out pdate);
                    string querydate = pdate.Year + "" + (pdate.Month > 9 ? pdate.Month + "" : "0" + pdate.Month) + "" + (pdate.Day > 9 ? pdate.Day + "" : "0" + pdate.Day);
                    new BenefitHelper.EnterPoint().Clear(querydate, db);
                    db.Dispose();
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        private bool couldVisitAdmin(int operId, BenefitHelper.DB.DBManager db, int tradeHistoryCount)
        {
            bool could = false;
            if (BenefitHelper.Sys.VisitPrower.HasAdminPrower(Convert.ToInt32(operId), db) || tradeHistoryCount == 0)
            {
                could = true;
            }
            return could;
        }

        private bool couldVisitWithoutLogin(string operId, int tradeHistoryCount)
        {
            bool could = false;
            if (!string.IsNullOrEmpty(operId) || tradeHistoryCount == 0)
            {
                could = true;
            }
            return could;
        }
    }
}
